package linkedlist;

/**
 * @author zhaoziqiang
 * @Description: 删除链表中等于给定值 val 的所有节点。（利用虚拟头结点）
 * <p>
 * 示例:
 * <p>
 * 输入:( dummyHead->) 1->2->6->3->4->5->6, val = 6
 * 输出: 1->2->3->4->5
 * @date 2020/8/31 15:33
 */
public class Solution2 {
    public static ListNode removeElements(ListNode head, int val) {
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next=head;
        ListNode cur = dummyHead;
        while (cur.next != null) {
            if (cur.next.val == val)
                cur.next = cur.next.next;
            else
                cur = cur.next;
        }
        return dummyHead.next;
    }

    public static void main(String[] args) {
        int[] arr=new int[]{1,2,6,3,4,6,7,6,9};
        ListNode listNode = new ListNode(arr);
        System.out.println(listNode);
        ListNode listNode1 = Solution2.removeElements(listNode, 6);
        System.out.println(listNode1);
    }
}
